package com.service;

import java.util.List;

import com.model.Role;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

/**   
 *   
 * @author zhanghusong   
 */
public class RoleServiceImpl extends AbstractDateService implements RoleService {

	@Override
	public boolean deleteRole(Role role) {
		String sql = "delete from t_role where id="+role.getId();
		logger.info("[SQL] [method:deleteRole] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base, sql);
	}

	@Override
	public List<Role> getRole() {
		String sql = "select id, rolename, menuvalue, remark, createtime, createuser from t_role";
		logger.info("[SQL] [method:getMenu] ["+sql+"]");
		ModelGenerator<Role> model = new ModelGenerator<Role>(Role.class);
		return model.generateList(base, sql.toString());
	}

	@Override
	public Role getRoleById(Role role) {
		String sql = "select id, rolename, menuvalue, remark, createtime," +
				" createuser from t_role where id="+role.getId();
		logger.info("[SQL] [method:getMenu] ["+sql+"]");
		ModelGenerator<Role> model = new ModelGenerator<Role>(Role.class);
		return model.generate(base, sql.toString());
	}

	@Override
	public boolean saveRole(Role role) {
		String sql = "insert into t_role(id, rolename, menuvalue, remark, createtime, createuser)" +
				"values("+getTableId("t_role")+
				", '"+role.getRoleName()+"', '"+role.getMenuValue()+
				"', '"+role.getRemark()+"', now(), '"+role.getCreateUser()+"')";
		logger.info("[SQL] [method:saveRole] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base, sql);
	}

	@Override
	public boolean updateRole(Role role) {
		String sql = "update t_role  set rolename = '"+role.getRoleName()+
		"', menuvalue = '"+role.getMenuValue()+"', remark = '"+role.getRemark()
		+"' where id="+role.getId();
		logger.info("[SQL] [method:updateRole] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base, sql);
	}

}
